package com.peakmain.wanandroid.mymodule.presenter;

import android.annotation.SuppressLint;

import com.peakmain.wanandroid.mymodule.contract.PublicAccountContract;
import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;
import com.peakmain.wanandroid.netmodule.bean.PublicAccountBean;
import com.peakmain.wanandroid.netmodule.bean.PublicAccountListBean;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/25
 * mail: 2726449200@qq.com
 * desiption:
 */
public class PublicAccontPresenter extends BasePresenter<PublicAccountContract.View> implements PublicAccountContract.Presenter{

   @Inject
   public  PublicAccontPresenter(){

   }
    @SuppressLint("CheckResult")
    @Override
    public void getPublicAccountList() {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getPublicAccountList()
                .compose(mView.<DataResponse<List<PublicAccountListBean>>>bindToLife())
                .compose(RxSchedulers.<DataResponse<List<PublicAccountListBean>>>applySchedulers())
                .subscribe(new Consumer<DataResponse<List<PublicAccountListBean>>>() {
                    @Override
                    public void accept(DataResponse<List<PublicAccountListBean>> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showPublicAccountList(dataResponse.getData());
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                        mView.hideLoading();
                    }
                });
    }
    @SuppressLint("CheckResult")
    @Override
    public void getPublicAccountData(int id, int page) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getPublicAccountData(id,page)
                .compose(mView.<DataResponse<PublicAccountBean>>bindToLife())
                .compose(RxSchedulers.<DataResponse<PublicAccountBean>>applySchedulers())
                .subscribe(new Consumer<DataResponse<PublicAccountBean>>() {
                    @Override
                    public void accept(DataResponse<PublicAccountBean> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showPublicAccountDataSuccess(dataResponse.getData());
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                        mView.hideLoading();
                    }
                });
    }
    @SuppressLint("CheckResult")
    @Override
    public void getPublicAccountSearchData(int id, int page, String key) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).getPublicAccountSearchData(id,page,key)
                .compose(mView.<DataResponse<PublicAccountBean>>bindToLife())
                .compose(RxSchedulers.<DataResponse<PublicAccountBean>>applySchedulers())
                .subscribe(new Consumer<DataResponse<PublicAccountBean>>() {
                    @Override
                    public void accept(DataResponse<PublicAccountBean> dataResponse) throws Exception {
                        if (dataResponse.getErrorCode() == 0) {
                            mView.showPublicAccountSearchSuccess(dataResponse.getData());
                        } else {
                            mView.showFaild(dataResponse.getErrorMessage());
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                        mView.hideLoading();
                    }
                });
    }
}
